import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart';

void main() {
  runApp(LightController());// 启动应用程序
}

class LightController extends StatefulWidget {// 创建一个StatefulWidget，拥有可变状态
  @override
  _LightControllerState createState() => _LightControllerState();// 创建其对应的状态类
}

class _LightControllerState extends State<LightController> {
  String _ip = '192.168.7.169'; // NodeMCU IP 地址

  void sendRequest(String endpoint) async {//请求函数
    var url = Uri.http(_ip, '/$endpoint');
    var response = await get(url);
    print('Response status: ${response.statusCode}');
  }

  @override
  Widget build(BuildContext context) {//界面设计
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Light Controller'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(//按钮组件-打开
                child: Text('Open'),
                onPressed: () {
                  sendRequest("open");
                },
              ),
              ElevatedButton(//按钮组件-关闭
                child: Text('Close'),
                onPressed: () {
                  sendRequest("close");
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}